﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using DLL;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace COM
{ 
    public partial class Form1 : Form
    { 
        public Form1()
        {
            InitializeComponent();
            Player1.Visible = false;
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtTargetSpeed.Clear();
            txtTargetAltitude.Clear();
            txtTargetDistance.Clear();
            txtTargetLocation.Clear();
            txtTargetSize.Clear();
            txtThreatLevel.Clear();
            txtMissileName.Clear();
            txtMissileQuantity.Clear();
            try
            {
                System.IO.File.Delete("../../STUBEVENT/COMEVENT.xml");
                File.Delete("../../../MIS/MISEvent.xml");
            }
            catch (FileNotFoundException file)
            {
            }
            MsgDisplayBox.Text = "Awaiting Targets";
            MsgDisplayBox.Text += Environment.NewLine;
        }

        bool ranOnce = false;
        Missile missile = new Missile();
        Status MISStatus = new Status();
        Status RADStatus = new Status();

        private void testbtnRandom_Click(object sender, EventArgs e)
        {
            int target;//, speed, altitude, distance, location;
            Target newTarget = new Target();
            

            Random random = new Random((int)DateTime.Now.Ticks);
            /**************************
             * target 1 = bird
             * target 2 = missile
             * target 3 = fighter jet
             * target 4 = commercial airliner
             * target 5 = battle ship
             * target 6 = cruise ship
             * ************************/
            //target = random.Next(1, 5);
            target = random.Next(2, 3);
            newTarget.TargetLocation = random.Next(0, 360);
            switch (target)
            {
                case 1:
                    {
                        newTarget.TargetSpeed = random.Next(1, 60);
                        //txtTargetSpeed.Text = Convert.ToString(newTarget.TargetSpeed);
                        newTarget.TargetAltitude = random.Next(1, 8000);
                        //txtTargetAltitude.Text = Convert.ToString(newTarget.TargetAltitude);
                        newTarget.TargetDistance = random.Next(5, 500);
                        //txtTargetDistance.Text = Convert.ToString(newTarget.TargetDistance);
                        //txtTargetLocation.Text = Convert.ToString(newTarget.TargetLocation);
                        newTarget.TargetSize = "Small";
                        //txtTargetSize.Text = newTarget.TargetSize;
                    }
                    break;
                case 2:
                    {
                        newTarget.TargetSpeed = random.Next(1000, 6000);
                        //txtTargetSpeed.Text = Convert.ToString(newTarget.TargetSpeed);
                        newTarget.TargetAltitude = random.Next(30, 125000);
                        //txtTargetAltitude.Text = Convert.ToString(newTarget.TargetAltitude);
                        //newTarget.TargetDistance = random.Next(100,1000);//real life values
                        newTarget.TargetDistance = random.Next(10, 50);
                        //txtTargetDistance.Text = Convert.ToString(newTarget.TargetDistance);
                        //txtTargetLocation.Text = Convert.ToString(newTarget.TargetLocation);
                        newTarget.TargetSize = "Small";
                        //txtTargetSize.Text = newTarget.TargetSize;
                    }
                    break;
                case 3:
                    {
                        newTarget.TargetSpeed = random.Next(1000, 2200);
                        //txtTargetSpeed.Text = Convert.ToString(newTarget.TargetSpeed);
                        newTarget.TargetAltitude = random.Next(30, 120000);
                        //txtTargetAltitude.Text = Convert.ToString(newTarget.TargetAltitude);
                        //newTarget.TargetDistance = random.Next(100, 2000); // real life values
                        newTarget.TargetDistance = random.Next(10, 50);
                        //txtTargetDistance.Text = Convert.ToString(newTarget.TargetDistance);
                        //txtTargetLocation.Text = Convert.ToString(newTarget.TargetLocation);
                        newTarget.TargetSize = "Medium";
                        //txtTargetSize.Text = newTarget.TargetSize;
                    }
                    break;
                case 4:
                    {
                        newTarget.TargetSpeed = random.Next(165, 750);
                        //txtTargetSpeed.Text = Convert.ToString(newTarget.TargetSpeed);
                        newTarget.TargetAltitude = random.Next(0, 50000);
                        //txtTargetAltitude.Text = Convert.ToString(newTarget.TargetAltitude);
                        newTarget.TargetDistance = random.Next(10, 2000);
                        //txtTargetDistance.Text = Convert.ToString(newTarget.TargetDistance);
                        //txtTargetLocation.Text = Convert.ToString(newTarget.TargetLocation);
                        newTarget.TargetSize = "Large";
                        //txtTargetSize.Text = newTarget.TargetSize;
                    }
                    break;
            }
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Target));
                TextWriter textWriter = new StreamWriter("../../STUBEVENT/COMEVENT.xml");
                serializer.Serialize(textWriter, newTarget);
                textWriter.Close();
                
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }

        }

        private void Target_Identify(object sender, EventArgs e)
        {
            if (txtTargetSpeed.Text == "")
                return;
            int speed = Convert.ToInt32(txtTargetSpeed.Text);
            int altitude = Convert.ToInt32(txtTargetAltitude.Text);
            String size = txtTargetSize.Text;

            if (speed < 60 && size == "Small")
            {
                txtTargetIdentity.Text = "Bird";
                txtThreatLevel.ForeColor = Color.Lime;
                txtThreatLevel.Text = "Low";
            }
            else if (speed > 60 && speed < 6000 && altitude < 125000 && size == "Small")
            {
                txtTargetIdentity.Text = "Missile";
                txtThreatLevel.ForeColor = Color.Red;
                txtThreatLevel.Text = "High";
            }
            else if (speed > 700 && speed < 2200 && altitude > 30 && altitude < 120000 && size == "Medium")
            {
                txtTargetIdentity.Text = "Fighter Jet";
                txtThreatLevel.ForeColor = Color.Yellow;
                txtThreatLevel.Text = "Medium";
            }
            else if (speed > 165 && speed < 750 && altitude > 0 && altitude < 50000 && size == "Large")
            {
                txtTargetIdentity.Text = "Commerical";
                txtThreatLevel.ForeColor = Color.Lime;
                txtThreatLevel.Text = "Low";
            }

            if (txtThreatLevel.Text == "High" || txtThreatLevel.Text == "Medium")
            {
                Distance_Calc.Enabled = true;
            }
            MsgDisplayBox.Text += "Target Locked In";
            MsgDisplayBox.Text += Environment.NewLine;
            MsgDisplayBox.Text += "Awaiting command";
            MsgDisplayBox.Text += Environment.NewLine;

           
        }

        private void Event_Serializer_Tick(object sender, EventArgs e)
        {
           
            Target newTarget = new Target();
            string FileToRead = "../../../RAD/RadarXML.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(Target));
            newTarget = (Target)XMLHandler.READXML(FileToRead, serializer);

            if (newTarget == null)
                return;

            txtTargetSpeed.Text = Convert.ToString(newTarget.TargetSpeed);
            txtTargetAltitude.Text = Convert.ToString(newTarget.TargetAltitude);
            txtTargetDistance.Text = Convert.ToString(newTarget.TargetDistance);
            txtTargetLocation.Text = Convert.ToString(newTarget.TargetLocation);
            txtTargetSize.Text = newTarget.TargetSize;

            double eventID = Convert.ToDouble(DateTime.Now.Ticks);
            File.Move("../../../RAD/RadarXML.xml", "../../../RAD/" + eventID + "Target.xml");

            Select_Missile.Enabled = true;            
        }

        private void Distance_Calc_Tick(object sender, EventArgs e)
        {
            int mph = Convert.ToInt32(txtTargetSpeed.Text);
            double distance = Convert.ToDouble(txtTargetDistance.Text);
            double altitude = Convert.ToDouble(txtTargetAltitude.Text);
            if (txtTargetIdentity.Text == "Missile")
            {
                if (distance > 0)
                {
                    double fallingRate = altitude / distance;
                    altitude -= fallingRate;
                    txtTargetAltitude.Text = String.Format("{0:0.0000}", altitude);
                }
            }
            double mps = mph * (1 / 60.0) * (1 / 60.0);
            distance -= mps;
            
            if (distance <= 0 && altitude <= 0)
            {
                if (txtTargetIdentity.Text == "Missile")
                {
                    txtTargetDistance.Text = "0";
                    txtTargetAltitude.Text = "0";
                    Distance_Calc.Enabled = false;
                    MessageBox.Show("You have been killed...Too bad! Next time attack the target faster!");
                    Application.Exit();
                }
            }
            txtTargetDistance.Text = String.Format("{0:0.0000}", distance);
            Event_Serializer.Enabled = false;
        }

        private void btnFire_Click(object sender, EventArgs e)
        {
            Target target = new Target();
            target.TargetAltitude = Convert.ToDouble(txtTargetAltitude.Text);
            target.TargetDistance = Convert.ToDouble(txtTargetDistance.Text);
            target.TargetLocation = Convert.ToInt32(txtTargetLocation.Text);
            target.TargetSpeed = Convert.ToInt32(txtTargetSpeed.Text);
            missile.MissileCommand = "Fire";
            
            
            XmlSerializer serializer = new XmlSerializer(typeof(Missile));
            string FileToWrite = "../../STUBEVENT/MISFIRE.xml";
            XMLHandler.WRITEXML(missile, FileToWrite, serializer);

            MsgDisplayBox.Text += "Missile fired \n";
            MsgDisplayBox.Text += Environment.NewLine;

            txtMissileStatus.Text = "Fired";
            Missile_Fired.Enabled = true;

            Player1.URL = @"C:\Users\prp5052\Desktop\missile-defense-project\MDP\MIS\MisImages\Jericho.wmv";  // for the video 
        }

        private void Missile_Fired_Tick(object sender, EventArgs e)
        {
            txtDistanceLeft.Visible = true;
            double targetDistance;
            double targetSpeed = Convert.ToDouble(txtTargetSpeed.Text);
            if (ranOnce == false)
                targetDistance = Convert.ToDouble(txtTargetDistance.Text);
            else
                targetDistance = Convert.ToDouble(txtDistanceLeft.Text);
            double missileSpeed = 7000;
            double missileSpeedperSecond = missileSpeed * (1 / 60.0) * (1 / 60.0);
            double targetSpeedperSecond = targetSpeed * (1 / 60.0) * (1 / 60.0);

            double distanceRemaining = targetDistance;

            distanceRemaining -= (missileSpeedperSecond + targetSpeedperSecond);
            txtDistanceLeft.Text = String.Format("{0:0.0000}", distanceRemaining);
            ranOnce = true;

            if (distanceRemaining <= 0)
            {
                Random random = new Random();
                int miss = random.Next(1, 101);
                if (miss < 10)
                {
                    MessageBox.Show("The missile missed the target!");
                }
                else
                {
                    Distance_Calc.Enabled = false;
                    Missile_Fired.Enabled = false;
                    txtTargetSpeed.Clear();
                    txtTargetAltitude.Clear();
                    txtTargetDistance.Clear();
                    txtTargetLocation.Clear();
                    txtTargetSize.Clear();
                    txtThreatLevel.Clear();
                    txtTargetIdentity.Clear();
                    txtMissileName.Clear();
                    txtMissileQuantity.Clear();
                    txtDistanceLeft.Text = "0";
                    txtMissileStatus.Clear();

                    MsgDisplayBox.Text += "Target Destroyed";
                    MsgDisplayBox.Text += Environment.NewLine;
                    Event_Serializer.Enabled = true;
                    MsgDisplayBox.Text += "Awaiting Targets";
                    MsgDisplayBox.Text += Environment.NewLine;
                    ranOnce = false;
                    btnFire.Enabled = false;
                    btnSelfDestruct.Enabled = false;
                }
            }            
        }

        private void Select_Missile_Tick(object sender, EventArgs e)
        {
            
            XmlSerializer serializer = new XmlSerializer(typeof(Missile));                
            string FileToRead = "../../../MIS/MISEvent.xml";                
            missile = (Missile)XMLHandler.READXML(FileToRead, serializer);
            btnFire.Enabled = true;
            btnSelfDestruct.Enabled = true;               
            File.Delete("../../../MIS/MISEvent.xml");

            if (missile == null)
                return;
            else
                Select_Missile.Enabled = false;

            txtMissileName.Text = Convert.ToString(missile.MissileName);
            txtMissileQuantity.Text = Convert.ToString(missile.MissileCount);
            txtMissileStatus.Text = missile.MissileCommand;            
        }

        private void btnSelfDestruct_Click(object sender, EventArgs e)
        {
            txtMissileStatus.Text = "Self Destructed";
            Distance_Calc.Enabled = false;
            txtDistanceLeft.Clear();
            txtMissileName.Clear();
            txtMissileQuantity.Clear();
            txtMissileStatus.Clear();
            Event_Serializer.Enabled = true;
            Missile_Fired.Enabled = false;
            ranOnce = false;
        }

        private void Status_Check_Tick(object sender, EventArgs e)
        {
            Status currentMISStatus = new Status();
            Status currentRADStatus = new Status();
            string MISFileToRead = "../../../MIS/MISStatus.xml";
            string RADFileToRead = "../../../RAD/RADStatus.xml";
            XmlSerializer reader = new XmlSerializer(typeof(Status));
            currentMISStatus = (Status)XMLHandler.READXML(MISFileToRead, reader);

            if (MISStatus.status == currentMISStatus.status)
            {
                missile_status.Text = "OFFLINE";
                missile_status.ForeColor = Color.Red;
            }
            else
            {
                missile_status.Text = "ONLINE";
                missile_status.ForeColor = Color.Lime;
                MISStatus.status = currentMISStatus.status;
            }

            currentRADStatus = (Status)XMLHandler.READXML(RADFileToRead, reader);

            if (RADStatus.status == currentRADStatus.status)
            {
                radar_status.Text = "OFFLINE";
                radar_status.ForeColor = Color.Red;
            }
            else
            {
                radar_status.Text = "ONLINE";
                radar_status.ForeColor = Color.Lime;
                RADStatus.status = currentRADStatus.status;
            }
        }
    }
}
